package com.example.wm.SanWar.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wm.SanWar.entity.AlliesandUser;
import com.example.wm.SanWar.entity.Pointsapplication;
import com.example.wm.SanWar.entity.Pointsrecord;
import com.example.wm.SanWar.mapper.PointsapplicationMapper;
import com.example.wm.SanWar.mapper.PointsrecordMapper;
import com.example.wm.SanWar.service.AlliesandUserService;
import com.example.wm.SanWar.service.PointsapplicationService;
import com.example.wm.SanWar.service.PointsrecordService;
import com.example.wm.common.Result;
import com.example.wm.common.ResultData;
import com.example.wm.utils.TsyUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.Jar;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/Pointsapplication")
public class PointsapplicationController {
    @Resource
    private TsyUtils tsyUtils;
    @Resource
    private PointsapplicationMapper pointsapplicationMapper;
    @Resource
    private PointsapplicationService pointsapplicationService;
    @Resource
    private AlliesandUserService alliesandUserService;
    @Transactional
    @PostMapping("/Insert")
    public Result Insert(@RequestBody Pointsapplication pointsapplication) {
        pointsapplication.setCreatetime(tsyUtils.getnowtime());
        LambdaQueryWrapper<AlliesandUser> lq=new LambdaQueryWrapper<>();;
        lq.eq(AlliesandUser::getName,pointsapplication.getAppointname());
        lq.eq(AlliesandUser::getStatus,"在盟");
        AlliesandUser one = alliesandUserService.getOne(lq);
        pointsapplication.setAppointnameid(one.getId());
        pointsapplicationMapper.insert(pointsapplication);
      return ResultData.success("插入成功");

    }
    @Transactional
    @PostMapping("/update")
    public Result update(@RequestBody Pointsapplication pointsapplication) {
        pointsapplication.setAppointtime(tsyUtils.getnowtime());
        pointsapplicationMapper.updateById(pointsapplication);
        return ResultData.success("修改成功");

    }
    @Transactional
    @PostMapping("/findpage")
    public Result findpage(@RequestBody JSONObject jsonObject) {
        LambdaQueryWrapper<Pointsapplication> lq=new LambdaQueryWrapper<>();
        lq.eq(Pointsapplication::getStatus,jsonObject.getString("status"));
        lq.eq(Pointsapplication::getAppointnameid,jsonObject.getString("appointnameid"));
        Page<Pointsapplication> pageInfo = new Page<>(jsonObject.getLong("page"),jsonObject.getLong("pageSize"));
        Page<Pointsapplication> list=pointsapplicationService.page(pageInfo,lq);
        if (list.getSize()>0){
            for(Pointsapplication pointsapplication : list.getRecords()){

                    LambdaQueryWrapper<AlliesandUser> lq2=new LambdaQueryWrapper<>();
                    lq2.eq(AlliesandUser::getId,pointsapplication.getCreatenameid());
                    lq2.eq(AlliesandUser::getStatus,"在盟");
                    AlliesandUser one = alliesandUserService.getOne(lq2);
                    if(!Objects.equals(one.getName(), pointsapplication.getCreatename())){
                        pointsapplication.setCreatename(one.getName());
                        pointsapplicationService.updateById(pointsapplication);
                    }

//                LambdaQueryWrapper<AlliesandUser> lq3=new LambdaQueryWrapper<>();
//                lq3.eq(AlliesandUser::getId,pointsapplication.getAppointnameid());
//                lq3.eq(AlliesandUser::getStatus,"在盟");
//                AlliesandUser one3 = alliesandUserService.getOne(lq2);
//                if(!Objects.equals(one3.getName(), pointsapplication.getAppointname())){
//                    pointsapplication.setAppointname(one3.getName());
//                }

            }
        }
        return ResultData.success(list);

    }
    @Transactional
    @PostMapping("/findpage2")
    public Result findpage2(@RequestBody JSONObject jsonObject) {

        if(!Objects.equals(jsonObject.getString("createnameid"), "")){
            LambdaQueryWrapper<Pointsapplication> lq=new LambdaQueryWrapper<>();
            lq.ne(Pointsapplication::getStatus,jsonObject.getString("status"));
            lq.eq(Pointsapplication::getAppointnameid,jsonObject.getString("appointnameid"));
            lq.eq(Pointsapplication::getCreatenameid,jsonObject.getString("createnameid"));
            lq.orderByDesc(Pointsapplication::getAppointtime);
            Page<Pointsapplication> pageInfo = new Page<>(jsonObject.getLong("page"),jsonObject.getLong("pageSize"));
            Page<Pointsapplication> list=pointsapplicationService.page(pageInfo,lq);
            if (list.getSize()>0){
                for(Pointsapplication pointsapplication : list.getRecords()){

                    LambdaQueryWrapper<AlliesandUser> lq2=new LambdaQueryWrapper<>();
                    lq2.eq(AlliesandUser::getId,pointsapplication.getCreatenameid());
                    lq2.eq(AlliesandUser::getStatus,"在盟");
                    AlliesandUser one = alliesandUserService.getOne(lq2);
                    if(!Objects.equals(one.getName(), pointsapplication.getCreatename())){
                        pointsapplication.setCreatename(one.getName());
                    }
                    LambdaQueryWrapper<AlliesandUser> lq3=new LambdaQueryWrapper<>();
                    lq3.eq(AlliesandUser::getId,pointsapplication.getAppointnameid());
                    lq3.eq(AlliesandUser::getStatus,"在盟");
                    AlliesandUser one3 = alliesandUserService.getOne(lq2);
                    if(!Objects.equals(one3.getName(), pointsapplication.getAppointname())){
                        pointsapplication.setAppointname(one3.getName());
                    }

                }
            }
            return ResultData.success(list);
        }else{
            LambdaQueryWrapper<Pointsapplication> lq=new LambdaQueryWrapper<>();
            lq.ne(Pointsapplication::getStatus,jsonObject.getString("status"));
            lq.eq(Pointsapplication::getAppointnameid,jsonObject.getString("appointnameid"));
            lq.orderByDesc(Pointsapplication::getAppointtime);

            Page<Pointsapplication> pageInfo = new Page<>(jsonObject.getLong("page"),jsonObject.getLong("pageSize"));
            Page<Pointsapplication> list=pointsapplicationService.page(pageInfo,lq);
            if (list.getSize()>0){
                for(Pointsapplication pointsapplication : list.getRecords()){

                    LambdaQueryWrapper<AlliesandUser> lq2=new LambdaQueryWrapper<>();
                    lq2.eq(AlliesandUser::getId,pointsapplication.getCreatenameid());
                    lq2.eq(AlliesandUser::getStatus,"在盟");
                    AlliesandUser one = alliesandUserService.getOne(lq2);
                    if(!Objects.equals(one.getName(), pointsapplication.getCreatename())){
                        pointsapplication.setCreatename(one.getName());
                    }
                    LambdaQueryWrapper<AlliesandUser> lq3=new LambdaQueryWrapper<>();
                    lq3.eq(AlliesandUser::getId,pointsapplication.getAppointnameid());
                    lq3.eq(AlliesandUser::getStatus,"在盟");
                    AlliesandUser one3 = alliesandUserService.getOne(lq2);
                    if(!Objects.equals(one3.getName(), pointsapplication.getAppointname())){
                        pointsapplication.setAppointname(one3.getName());
                    }
                }
            }
            return ResultData.success(list);

        }



    }
}
